//
// gcssai_database.cpp
// gcs
// Grand Central Station, controlling process of the Protea Application Server / Business Logic Transactions for SAI Database functionality
// Copyright (C) 2003, 2004, 2005 eXegeSys, Inc.
// Copyright (C) 2008 Bruce A. James
//
// This program is part of the Protea Project.
//
// The Protea Project is free software; you can redistribute it and/or modify it 
// under the terms of the GNU General Public License as published by the 
// Free Software Foundation, either version 3 of the License, or (at your 
// option) any later version.
//
// The Protea Project is distributed in the hope that it will be useful, but 
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
// for more details.
// 
// You should have received a copy of the GNU General Public License along 
// with this program.  If not, see <http://www.gnu.org/licenses/>.
// 
// Please send correspondence to:
// 
// theproteaproject@gmail.com
//

#include "../SDK/ToolsInterface.h"
#include "../AppHandler/parms_xml.h"
#include "gcs.h"


//@ SEC 3
//@ CHA Database Add
//@ COD DatabaseAdd
//@ GEN Use DatabaseAdd to add a database record based on primary keys

void XCGCS::DatabaseAdd( XCBuffer* buffer, XCTransactionVars* vars ) {
	TRANS_INFUNC( DatabaseAdd );

	XCSQLGen* query = vars->query;
	query->MakeFull( SQL_STMT_INSERT, buffer );
	vars->dbtxn->ExecuteSQL( query, SQL_NO_RESULT );

	TRANS_OUTFUNC();
};


//@ SEC 3
//@ CHA Database Remove
//@ COD DatabaseRemove
//@ GEN Use DatabaseRemove to remove a database record based on primary keys

void XCGCS::DatabaseRemove( XCBuffer* buffer, XCTransactionVars* vars ) {
	TRANS_INFUNC( DatabaseRemove );

	XCSQLGen* query = vars->query;
	query->MakeFull( SQL_STMT_DELETE, buffer );
	vars->dbtxn->ExecuteSQL( query, SQL_NO_RESULT );

	TRANS_OUTFUNC();
};



//@ SEC 3
//@ CHA Database Change
//@ COD DatabaseChange
//@ GEN Use DatabaseChange to remove a database record based on primary keys

void XCGCS::DatabaseChange( XCBuffer* new_buffer, XCBuffer* original_buffer, XCTransactionVars* vars ) {
	TRANS_INFUNC( DatabaseChange );

	XCSQLGen* query = vars->query;
	query->MakeFull( SQL_STMT_UPDATE, new_buffer, original_buffer );
	vars->dbtxn->ExecuteSQL( query, SQL_NO_RESULT );

	TRANS_OUTFUNC();
};

//@ SEC 3
//@ CHA Database Retrieve
//@ COD DatabaseRetrieve
//@ GEN Use DatabaseRetrieve to retrieve a database record based on primary keys

bool XCGCS::DatabaseRetrieve( XCBuffer* buffer, XCTransactionVars* vars ) {
	TRANS_INFUNC( JobScheduleRetrieve );

	XCDBStatement* stmt = NULL;
	XCSQLGen* query = vars->query;
	bool result;

	//@ Attempt to read JOB_SCHEDULE record
	try {
		query->MakeFull( SQL_STMT_SELECT, buffer );
		stmt = vars->dbtxn->ExecuteSQL( query );
		result = !stmt->Eof();
		App->CleanupObjects( 1, &stmt );
	} catch( ... ) {
		App->CleanupObjects( 1, &stmt );
		throw;
	};

	TRANS_OUTFUNCRET( result );
};


